<template>
  <div class="admin">


    <div class="ibox-content no-border no-padding-bottom">
      <!--搜索栏-->
      <el-col>
        <el-form :model="filterForm" :inline="true" size="mini" label-width="80px" ref="filterForm">
          <el-form-item label="用户id">
            <el-input v-model="filterForm.id" width="150px" placeholder="精确匹配用户id"></el-input>
          </el-form-item>
          <el-form-item label="登录账号">
            <el-input v-model="filterForm.login_name" width="150px" placeholder="登录账号"></el-input>
          </el-form-item>
          <el-form-item label="真实姓名">
            <el-input v-model="filterForm.real_name" width="150px" placeholder="真实姓名"></el-input>
          </el-form-item>
          <el-form-item label="移动电话">
            <el-input v-model="filterForm.phone" width="150px" placeholder="移动电话"></el-input>
          </el-form-item>
          <el-form-item label="电子邮箱">
            <el-input v-model="filterForm.email" width="150px" placeholder="电子邮箱"></el-input>
          </el-form-item>
          <el-form-item label="账号状态">
            <el-select width="150px" v-model="filterForm.status" placeholder="账号状态">
              <el-option label="全部" value=""></el-option>
              <el-option label="启用" value="10"></el-option>
              <el-option label="禁用" value="20"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button @click.native="filterClick" type="primary">查询</el-button>
            <el-button @click.native="resetFilterForm('filterForm')">重置</el-button>
          </el-form-item>
          <div class="hr-line-dashed"></div>
          <br/>
          <el-form-item size="small">
            <el-button @click="addAdminAcount" type="primary" plain icon="el-icon-plus">添加管理员账号</el-button>
            <el-button @click="multiplDelete" type="danger" plain icon="el-icon-delete">删除管理员账号</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </div>
    <div class="ibox-content">
      <!--表格-->
      <el-table
        ref="multipleTable"
        :data="arrList"
        size="medium"
        border
        highlight-current-row
        tooltip-effect="dark"
        style="width: 100%"
        :max-height="500"
        v-loading="tableLoading"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55"></el-table-column>
        <el-table-column prop="id" label="ID" width="80" sortable fixed="left"></el-table-column>
        <el-table-column prop="login_name" label="登录账号" sortable></el-table-column>
        <el-table-column prop="real_name" label="真实姓名" sortable></el-table-column>
        <el-table-column prop="phone" label="移动电话" sortable></el-table-column>
        <el-table-column prop="email" label="电子邮箱" sortable></el-table-column>
        <el-table-column prop="status" label="账号状态">
          <template slot-scope="scope">
            <span v-if="scope.row.status==10" class="green">启用</span>
            <span v-if="scope.row.status==20" class="red">禁用</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="190" fixed="right">
          <template slot-scope="scope">
            <el-tooltip @click.native="handleEdit(scope.row)" :enterable="false" effect="dark" content="编辑"
                        placement="top-start">
              <el-button type="primary" plain size="mini" icon="el-icon-edit"></el-button>
            </el-tooltip>
            <el-tooltip @click.native="toSettingAdminRole(scope.row)" :enterable="false" effect="dark" content="设置角色"
                        placement="top-start">
              <el-button type="primary" plain size="mini" icon="el-icon-setting"></el-button>
            </el-tooltip>
            <el-tooltip @click.native="handleDelete(scope.row)" :enterable="false" effect="dark" content="删除"
                        placement="top-start">
              <el-button type="danger" plain size="mini" icon="el-icon-delete"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>

      <!--分页工具条-->
      <div v-show="arrList.length>0" style="margin-top: 18px;text-align: center">
        <el-pagination
          @current-change="handleCurrentChange"
          :current-page="pageInfo.currPage"
          :page-sizes="[20]"
          :page-size="pageInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pageInfo.totalCount">
        </el-pagination>
      </div>
    </div>


    <!--    修改/添加-->
    <el-dialog width="26%" :title="editFormTitle" :visible.sync="editFormVisible" @close="closeDialog"
               :close-on-click-modal="false">
      <el-form :model="editForm" :rules="editFormRules" ref="editForm" size="small">
        <el-form-item label="账号" prop="login_name">
          <el-input v-model="editForm.login_name"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="">
          <el-input v-model="editForm.password"></el-input>
          <span v-if="isAdd" style="color: #999;font-size: 12px">不输入密码，则使用“123abc”做为初始密码</span>
          <span v-else style="color: #999;font-size: 12px">不修改密码请留空</span>
        </el-form-item>
        <el-form-item label="电话" prop="phone">
          <el-input v-model="editForm.phone"></el-input>
        </el-form-item>
        <el-form-item label="真实姓名" prop="real_name">
          <el-input v-model="editForm.real_name"></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <br>
          <el-radio-group v-model="editForm.status">
            <el-radio class="radio" :label="10">启用</el-radio>
            <el-radio class="radio" :label="20">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="电子邮箱">
          <el-input v-model="filterForm.email"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="small" @click.native="closeDialog">取消</el-button>
        <el-button size="small" type="primary" @click.native="editSubmit('editForm')">提交</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import {adminList, addAdmin, updateAdmin, deleteAdmin} from "../../api/admin";

  export default {
    name: "Admin",
    data() {
      return {
        editFormVisible: false,
        isAdd: true,
        editFormTitle: '',
        editForm: {
          password: 'abc123',
          status: 10
        },
        filterFormRules: {},
        editFormRules: {
          login_name: [{required: true, message: '不能为空'}],
          real_name: [{required: true, message: '不能为空'}],
          phone: [{required: true, message: '不能为空'}],
          status: [{required: true, message: '不能为空'}],
        },
        arrList: [],
        pageInfo: {
          totalPage: 0,// 总页数
          totalCount: 0,// 总条数
          pageSize: 0,// 每页条数
          currPage: 1// 当前页
        },
        filterForm: {},
        tableLoading: false,
        tableSelectData: [],
        loginRules: {
          username: [{required: true, trigger: 'blur',}],
          password: [{required: true, trigger: 'blur',}]
        },
      }
    },
    created() {
      this.initData();
    },
    methods: {
      initData() {
        this.tableLoading = true;
        adminList({
          page: this.pageInfo.currPage,
          ...this.filterForm
        }).then(res => {
          this.tableLoading = false;
          const {list, page} = res.data;
          this.arrList = list;
          this.pageInfo = page;
        }).catch(() => {
          this.tableLoading = false
        })
      },

      //查询
      filterClick() {
        this.pageInfo.currPage = 1;
        this.initData();
      },
      //重置
      resetFilterForm(form) {
        this.$refs[form].resetFields();
        this.filterForm = {};
        this.pageInfo.currPage = 1;
        this.initData();
      },
      //单个删除
      handleDelete(row) {
        this.$confirm('', `确定要删除${row.login_name}管理员吗`, {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }).then(() => {
          this.commonDeleteMethods(row.data_code)
        })
      },
      commonDeleteMethods(id) {
        deleteAdmin({data_code: id}).then(res => {
          this.$message.success(res.msg);
          this.initData();
        }).catch(error => {

        })
      },
      //可以多个删除
      multiplDelete() {
        if (this.tableSelectData.length > 0) {
          let ids = [];
          this.tableSelectData.forEach((item) => {
            ids.push(item.data_code)
          });
          if (ids.length > 0) {
            this.$confirm('确定要删除选择的管理员吗', '提示', {
              confirmButtonText: '确定',
              cancelButtonText: '取消',
            }).then(() => {
              this.commonDeleteMethods(ids.toString())
            })
          }
        } else {
          this.$message.warning('请选择要删除的管理员')
        }
      },

      //勾选
      handleSelectionChange(data) {
        this.tableSelectData = data;
      },
      handleCurrentChange(val) {
        // console.log(val);
        this.pageInfo.currPage = val;
        this.initData();
      },

      handleEdit(row) {

        this.editFormTitle = '编辑管理员账号' + row.login_name;
        this.editForm = Object.assign({}, row);
        this.isAdd = false;
        this.editFormVisible = true;
      },
      closeDialog() {
        this.editFormVisible = false;
        this.resetForm();
      },
      editSubmit(form) {
        this.$refs[form].validate(valida => {
          if (valida) {
            if (this.isAdd) {
              addAdmin({
                ...this.editForm
              }).then(res => {
                this.editFormVisible = false;
                this.$message.success(res.msg);
                this.initData();
                this.editForm = {
                  password: 'abc123',
                  status: 10
                }
              }).catch(error => {

              })
            } else {
              updateAdmin({
                ...this.editForm
              }).then(res => {
                this.editFormVisible = false;
                this.$message.success(res.msg);
                this.initData();
                this.editForm = {
                  password: 'abc123',
                  status: 10
                }
              }).catch(error => {

              })
            }

          }
        });
      },
      //清楚form 验证
      resetForm() {
        this.$refs['editForm'].clearValidate();
      },
      //添加管理员账号
      addAdminAcount() {
        this.editFormTitle = '添加账号';
        this.editFormVisible = true;
        this.isAdd = true;
      },
      toSettingAdminRole(row) {
        this.$router.push({name: 'SettingAdminRole', query: {code: row.data_code}})
      }
    }
  }
</script>

<style scoped lang="scss">
  @import "~element-ui/packages/theme-chalk/src/common/var";

  .red {
    color: $--color-danger;
  }

  .green {
    color: $--color-primary;
  }

  .warning {
    color: $--color-warning;
  }
</style>

<style lang="scss">
  .el-dialog__body {
    padding: 10px 20px;
  }
</style>
